package com.haoan.index.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.haoan.common.annotation.Anonymous;
import com.haoan.index.common.enums.ValiFlag;
import com.haoan.index.domain.AnnualPlace;
import com.haoan.index.domain.Place;
import com.haoan.index.service.AnnualPlaceService;
import com.haoan.index.service.PlaceService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haoan.common.annotation.Log;
import com.haoan.common.core.controller.BaseController;
import com.haoan.common.core.domain.AjaxResult;
import com.haoan.common.enums.BusinessType;
import com.haoan.index.domain.AnnualPass;
import com.haoan.index.service.AnnualPassService;
import com.haoan.common.utils.poi.ExcelUtil;
import com.haoan.common.core.page.TableDataInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 年卡阵地Controller
 *
 * @author autoGen
 * @date 2025-01-03
 */
@Api(value="年卡阵地",tags = "年卡阵地")
@RestController
@RequestMapping("/index/annualPass")
public class AnnualPassController extends BaseController
{
    @Resource
    private AnnualPassService annualPassService;
    @Resource
    private AnnualPlaceService annualPlaceService;
    @Resource
    private PlaceService placeService;

    /**
     * 查询年卡阵地列表
     */
    @ApiOperation("查询年卡阵地列表")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(AnnualPass annualPass)
    {
        startPage();
        List<AnnualPass> list = annualPassService.selectAnnualPassList(annualPass);
        return getDataTable(list);
    }

    /**
     * 导出年卡阵地列表
     */
    @ApiOperation("导出年卡阵地列表")
    @PreAuthorize("@ss.hasPermi('index:annualPass:export')")
    @Log(title = "年卡阵地", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AnnualPass annualPass)
    {
        List<AnnualPass> list = annualPassService.selectAnnualPassList(annualPass);
        ExcelUtil<AnnualPass> util = new ExcelUtil<AnnualPass>(AnnualPass.class);
        util.exportExcel(response, list, "年卡阵地数据");
    }

    /**
     * 获取年卡阵地详细信息
     */
    @ApiOperation("获取年卡阵地详细信息")
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,Place placeRef)
    {
        AnnualPass annualPass = annualPassService.selectAnnualPassById(id);
        LambdaQueryWrapper<AnnualPlace> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AnnualPlace::getAnnualId,id);
        if (null != placeRef.getName() || null != placeRef.getRegion()  || null !=placeRef.getGrade()){
            LambdaQueryWrapper<Place> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(placeRef.getName() != null && !placeRef.getName().equals(""), Place::getName, placeRef.getName());
            wrapper.like(placeRef.getRegion() != null && !placeRef.getRegion().equals(""), Place::getRegion, placeRef.getRegion());
            wrapper.eq(placeRef.getGrade() != null && !placeRef.getGrade().equals(""), Place::getGrade, placeRef.getGrade());
            wrapper.eq(Place::getValiFlag, ValiFlag.VALID );
            List<Place> places = placeService.list(wrapper);

            List<Long> list = new ArrayList<>();
            list.add(0L);
            places.forEach(e->{
                list.add(e.getId());
            });
            queryWrapper.in(AnnualPlace::getPlaceId,list);

        }

        List<AnnualPlace> annualPlaceList = annualPlaceService.list(queryWrapper);

        for (AnnualPlace annualPlace : annualPlaceList) {
            Place place = placeService.selectPlaceById(annualPlace.getPlaceId());
            annualPlace.setRegion(place.getRegion());
            annualPlace.setPrice(place.getPrice());
            annualPlace.setPlaceName(place.getName());
            annualPlace.setPicture(place.getPicture());
            annualPlace.setGrade(place.getGrade());
        }
        annualPass.setAnnualPlaceList(annualPlaceList);
        return success(annualPass);
    }

    /**
     * 新增年卡阵地
     */
    @ApiOperation("新增年卡阵地")
    @PreAuthorize("@ss.hasPermi('index:annualPass:add')")
    @Log(title = "年卡阵地", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AnnualPass annualPass)
    {
        return toAjax(annualPassService.insertAnnualPass(annualPass));
    }

    /**
     * 修改年卡阵地
     */
    @ApiOperation("修改年卡阵地")
    @PreAuthorize("@ss.hasPermi('index:annualPass:edit')")
    @Log(title = "年卡阵地", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AnnualPass annualPass)
    {
        return toAjax(annualPassService.updateAnnualPass(annualPass));
    }

    /**
     * 删除年卡阵地
     */
    @ApiOperation("删除年卡阵地")
    @PreAuthorize("@ss.hasPermi('index:annualPass:remove')")
    @Log(title = "年卡阵地", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(annualPassService.deleteAnnualPassByIds(ids));
    }
}
